package com.leiou.stu_reg;

import com.leiou.pojo.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;

//学生注册
@WebServlet("/stureg")
public class stuRegHandler extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
//        用什么方式显示汉字
        resp.setContentType("text/html;charset=UTF-8");

        stuRegDao stuRegDao = new stuRegDaoImpl();
        PrintWriter out = resp.getWriter();

        try {
            Student student = new Student();
            student.setUsername(req.getParameter("username"));
            student.setPassword(req.getParameter("password"));
            student.setName(req.getParameter("name"));
            student.setGender(req.getParameter("gender"));
            student.setIdCard(req.getParameter("idCard"));
            student.setPhone(req.getParameter("phone"));
            student.setEmail(req.getParameter("email"));
            student.setEmergencyContact(req.getParameter("emergencyContact"));
            System.out.println("========="+student);
            String birthDateStr = req.getParameter("birthDate");
            LocalDate birthDate = LocalDate.parse(birthDateStr);
            if (birthDateStr != null && !birthDateStr.isEmpty()) {
//                LocalDate birthDate = LocalDate.parse(birthDateStr);
                student.setBirthDate(birthDate);
            }
            System.out.println("+++++"+student);
            int result = stuRegDao.insertStudent(student);
            System.out.println("``````");
            if (result > 0) {
                out.print("注册成功");
            } else {
                out.print("注册失败");
            }

        } catch (Exception e) {
            e.printStackTrace();
            out.print("系统错误: " + e.getMessage());
        }
    }
}
